package com.jiukuaidao.zhulin.expand.baidu.location;

import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.util.Utils;
import com.jiukuaidao.zhulin.entity.ApiGeoCoderInfo;
import com.jiukuaidao.zhulin.expand.baidu.EventBusBDLocationInfo;
import com.jiukuaidao.zhulin.expand.baidu.EventBusLocationFailedInfo;
import com.jiukuaidao.zhulin.expand.baidu.ICallBack;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;
import com.jiukuaidao.zhulin.util.BDUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Author : BinarySatan
 * Date : 2016/5/24
 * Description : 定位监听
 */
public class LocationListener implements BDLocationListener {


    @Override
    public void onReceiveLocation(BDLocation location) {

        int locationType = location.getLocType();

        if (locationType == BDLocation.TypeOffLineLocation //离线定位失败
                || locationType == BDLocation.TypeServerError //服务端网络定位失败
                || locationType == BDLocation.TypeNetWorkException //网络不同导致定位失败,检查网络是否通畅
                || locationType == BDLocation.TypeCriteriaException //无法获取有效定位依据导致定位失败,一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机
                || (locationType >= 501 && locationType <= 700)) {//key 验证错误
            EventBus.getDefault().post(new EventBusLocationFailedInfo());
            return;
        }


        //       BDUtils.locationMap(location.getLongitude(), location.getLatitude());

        sendLocationMessage(location);

        Location.getInstance().stop();
    }

    /**
     * 发送定位消息
     *
     * @param location
     */
    private void sendLocationMessage(BDLocation location) {
        // 备用bean
        EventBusBDLocationInfo locationInfo = new EventBusBDLocationInfo();
        locationInfo.setLatitude(location.getLatitude());
        locationInfo.setLongitude(location.getLongitude());
        locationInfo.setAddress(location.getAddrStr());
        locationInfo.setCity(location.getCity());
        locationInfo.setCountry(location.getCountry());
        locationInfo.setDistrict(location.getDistrict());
        locationInfo.setProvince(location.getProvince());
        locationInfo.setStreet(location.getStreet());
        locationInfo.setType(location.getLocType());
        //发送定位消息
        EventBus.getDefault().post(locationInfo);

        BDUtils.geocoding(location.getLongitude(), location.getLatitude(), new ICallBack<ApiGeoCoderInfo>() {
            @Override
            public void onSuccess(ApiGeoCoderInfo apiGeoCoderInfo) {
                if (apiGeoCoderInfo != null) {
                    //当前位置存入首选项
                    AddressInfoUtil.cleanAddressInfo();
                    PreAddressBean mAddressInfo = new PreAddressBean();
                    mAddressInfo.longitude = apiGeoCoderInfo.result.location.lng;
                    mAddressInfo.latitude = apiGeoCoderInfo.result.location.lat;
                    mAddressInfo.city = apiGeoCoderInfo.result.addressComponent.city;
                    mAddressInfo.cityCode = apiGeoCoderInfo.result.cityCode;
                    mAddressInfo.address = apiGeoCoderInfo.result.sematic_description;
                    AddressInfoUtil.saveAddressInfo(mAddressInfo);
                    EventBus.getDefault().post(apiGeoCoderInfo);
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                if (Config.Debug) {
                    Toast.makeText(Utils.getContext(), errorMsg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
